package com.coolwen.controller;

import com.coolwen.entity.Challenge;
import com.coolwen.entity.User;
import com.coolwen.service.ChallengeService;
import com.coolwen.service.UserTeamService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpSession;

/**
 * Created by Zero on 2016/12/13.
 */
@Controller
@RequestMapping(value = {"/challenge/Challenge"})
public class ScoreController {


    @Autowired
    ChallengeService challengeService;

    @Autowired
    UserTeamService userTeamService;

    /**
     * 引入log4j,进行日志打印
     */
    private static final Logger LOGGER = Logger.getLogger(ChallengeController.class);

    /**
     * 设置跳转到约战结束界面
     *
     * @return
     */
    @RequestMapping(value = "/list/over", method = RequestMethod.GET)
    public String Over(Model model, HttpSession session) {

        // 通过user的session，读取teamId

        User user = (User) session.getAttribute("loginUser");

        int teamId = userTeamService.findUserTeamByUserID(user.getUserId()).getTeamId();

        //获取符合要求的约战信息
        model.addAttribute("challenge", challengeService.findByConfirmState("2", teamId));

        model.addAttribute("challengeAway", challengeService.findChallengeByReceiveId("2", teamId));

        return "challenge/challengeOver";
    }


    /**
     * 获取主客场名字
     *
     * @param challengeId
     * @param model
     * @return
     */


    @RequestMapping(value = "/list/over/update/{challengeId}", method = RequestMethod.GET)
    public String challengeScore(@PathVariable int challengeId, Model model) {

        model.addAttribute("challengeScore", challengeService.findChallengeById(challengeId));

        return "challenge/challengeEntryScore";
    }


    /**
     * 录入比分
     * 插入主客场比分
     * 改变录入比分状态码为2
     *
     * @param challengeScore
     * @return
     */

    @RequestMapping(value = "/list/over/{challengeId}", method = RequestMethod.POST)
    public String challengeScore(@PathVariable int challengeId, @Validated Challenge challengeScore, Model model) {

        challengeScore.setChallengePresent(Short.valueOf("2"));

        model.addAttribute("challengeScore", challengeService.updateGameScore(challengeScore, challengeId));

        return "redirect:/challenge/Challenge/list/over";
    }


    /**
     * 获取主客场名字
     *
     * @param challengeId
     * @param model
     * @return
     */


    @RequestMapping(value = "/list/over/present/{challengeId}", method = RequestMethod.GET)
    public String challengePresent(@PathVariable int challengeId, Model model) {

        model.addAttribute("challengeScore", challengeService.findChallengeById(challengeId));

        return "challenge/challengeUpdateScore";
    }


    /**
     * 修改比分
     * 插入主客场比分
     * 改变录入比分状态码为3
     *
     * @param challengeScore
     * @return
     */

    @RequestMapping(value = "/list/over3/{challengeId}", method = RequestMethod.POST)
    public String challengePresent(@PathVariable int challengeId, @Validated Challenge challengeScore, Model model) {

        challengeScore.setChallengePresent(Short.valueOf("3"));

        model.addAttribute("challengeScore", challengeService.updateGameScore(challengeScore, challengeId));

        return "redirect:/challenge/Challenge/list/over";
    }


    /**
     * 确认比分
     * 插入主客场比分
     * 改变录入比分状态码为4
     * 改变输赢状态码
     */

    @RequestMapping(value = "/list/over/present4/{challengeId}/{challengeResult}")
    public String challengePresent4(@PathVariable int challengeId, @PathVariable String challengeResult) {

        Challenge challengeScore = new Challenge();

        challengeScore.setChallengePresent(Short.valueOf("4"));  //录入比分状态码改为4

        challengeService.updateGameScore(challengeScore, challengeId);  //修改录入状态码

        challengeService.updateChallengeResultByChallengeId(challengeResult, challengeId);  //修改输赢状态码

        return "redirect:/challenge/Challenge/list/over";
    }


}
